﻿Imports Business
Imports Entities
Imports DataAccess
Public Class VentasPOS

    Private Sub VentasPOS_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        With cFormaPago
            .DataSource = FormaPagoBL.List
            .ValueMember = "FORMAPAGO"
            .DisplayMember = "DESCRIPCION"
            .SelectedIndex = -1
        End With
        Titulo()
    End Sub

    
    Private Sub Titulo()
        With gInforme
            Dim lista = VentaBL.ListByPedidoPOSId
            If IsNothing(lista) Then
                Util.Mensaje("No Existen Ventas de POS Pendientes")
                Exit Sub
            End If
            Dim checkCell As DataGridViewCell = New DataGridViewCheckBoxCell

            Dim checkColumn As New DataGridViewCheckBoxColumn With {.HeaderText = "Recibir Pago",
                                                      .Name = "RecibirPago",
                                                      .ReadOnly = False,
                                                      .ValueType = Type.GetType("System.Int64"),
                                                      .Visible = True,
                                                      .Width = 50,
                                                      .Resizable = False,
                                                    .CellTemplate = checkCell}
            .DataSource = lista
            .Columns("ventaid").Visible = False
            .Columns("Fecha").HeaderText = "Fecha Venta"
            .Columns("Hora").HeaderText = "Hora Venta"
            .Columns("PedidoPOSId").HeaderText = "Número Pedido POS"
            .Columns("ValorDescuento").HeaderText = "Valor Descuento"
            .Columns("ValorDescuento").DefaultCellStyle.Format = "$ ###,##0"
            If Not .Columns.Contains("RecibirPago") Then
                .Columns.Add(checkColumn)
            End If
        End With
    End Sub
    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        Dispose()
    End Sub
    Private Function ValidarCheck() As Boolean
        Dim ret As Boolean = False
        For Each ven As DataGridViewRow In gInforme.Rows
            If Not IsNothing(ven.Cells("RecibirPago").Value) AndAlso ven.Cells("RecibirPago").Value = 1 Then Return True
        Next
        Return ret
    End Function
    Private Sub bRecibirPago_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bRecibirPago.Click
        If Not ValidarCheck() Then
            Util.Mensaje("Debe Seleccionar al Menos una Venta")
            Exit Sub
        End If
        If Not cFormaPago.SelectedIndex > -1 Then
            Util.Mensaje("Debe Seleccionar Forma de Pago")
            cFormaPago.Focus()
            Exit Sub
        End If
        Dim hash As New Dictionary(Of Long, Long)
        Dim checkcell As New DataGridViewCheckBoxCell
        For Each ven As DataGridViewRow In gInforme.Rows
            checkcell = DirectCast(ven.Cells("RecibirPago"), DataGridViewCheckBoxCell)
            If ven.Cells("RecibirPago").Value = 1 Then
                Dim pago As New pagos
                With pago
                    .FECHA = Now
                    .FORMAPAGO = cFormaPago.SelectedValue
                    .MONTO = CLng(gInforme.Item("ValorDescuento", ven.Index).Value)
                    .TIPO = Util.TipoPago.DesctoPOS
                End With
                pago = PagosBL.Save(pago)
                VentaBL.UpdateDevuelto(CLng(gInforme.Item("Ventaid", ven.Index).Value))
            End If
        Next
        Util.Mensaje("Pagos Registrados")
        Titulo()
    End Sub

    Private Sub oSelect_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oSelect.CheckedChanged
        For Each ven As DataGridViewRow In gInforme.Rows
            If oSelect.Checked Then
                ven.Cells("RecibirPago").Value = 1
            Else
                ven.Cells("RecibirPago").Value = 0
            End If
        Next
    End Sub
End Class